package {
	import org.flixel.*;
	
	public class Projectile extends FlxSprite {
		[Embed(source = "/../media/Dot.png")] private var ImgDot:Class;
		
		protected var bounceVelocX:Number;
		protected var bounceVelocY:Number;
		
		public function Projectile (X:Number, Y:Number) {
			super(X, Y);
			loadGraphic(ImgDot, false);
			acceleration.y = 400; //gravity
		}
		
		public function fire(mouseXY:FlxPoint,playerVelocity:FlxPoint):void {
			var v:Number = 200; //initial velocity
			var a:Number = mouseXY.x - x + 180;
			var b:Number = mouseXY.y - y + 150;
			
			/*First method -- didn't work too hot
			var theta:Number = Math.atan(b/a);
			velocity.x = v * Math.cos(theta);
			velocity.y = v * Math.sin(theta);
			*/
			
			/*Second method*/
			//distance between 2 coordinates = sqrt[(x2-x1)^2 + (y2-y1)^2]
			var d:Number = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
			velocity.x = ((v * a) / d)+playerVelocity.x;
			velocity.y = ((v * b) / d)+playerVelocity.y;
		}
		
		override public function update():void {
			
			if (!isTouching(WALL)) {
				bounceVelocX = velocity.x;
			}
			
			else {
				if (justTouched(WALL)) {
					velocity.x = -bounceVelocX;
					bounceVelocX = -bounceVelocX;
				}
			}
			
			if (!isTouching(FLOOR)) {
				bounceVelocY = velocity.y;
			}
			
			else {
				if (justTouched(FLOOR))
					velocity.y = -bounceVelocY * .8;	//bounce off floors
				else
					acceleration.x = -2 * velocity.x;	//friction between floor and projectile
			}
		}
	}
}